package com.tyersoft.kickcalendar.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tyersoft.kickcalendar.R;
import com.tyersoft.kickcalendar.calendar.Event;
import com.tyersoft.kickcalendar.calendar.EventRef;

public class SoftTaskAdapter extends TaskAdapter {

	private final int name_width;
	private final int pior_width;
	private final int height;

	public SoftTaskAdapter(Context context, ArrayList<Event> events) {
		super(context, events);

		Resources resources = context.getResources();

		name_width = resources.getDimensionPixelSize(R.dimen.table_alldayevent_name_width);
		pior_width = resources.getDimensionPixelSize(R.dimen.table_alldayevent_prior_width);
		
		height = resources.getDimensionPixelSize(R.dimen.table_alldayevent_height);
	}

	@Override
	public int getColumnCount() {
		return 1+1; // task name + prior
	}

	@Override
	public int getWidth(int column) {
		switch (column) {
			case -1: return name_width;
			case 0: return pior_width;
			default: return 0;
		}
	}

	@Override
	public int getHeight(int row) {
		return height;
	}

	@Override
	public String getCellString(int row, int column) {
		
		// FIRST ROW = HEADER
		if (row == -1) {
			switch (column) {
				case -1: return getContext().getString(R.string.alldayevent_table_title);
				case 0:  return getContext().getString(R.string.alldayevent_table_prior);
				default: return String.valueOf(column-1);
			}
		}
		
		if (row < events.size()) {
			switch (column) {
				// FIRST COLUMN = EVENT NAME
				case -1: return events.get(row).title;
				// SECOND COLUMN = EVENT TIME
				case 0: return "";
				default: return "";
			}
		}
		return "(" + row + "," + column + ")";
	}

	@Override
	public int getLayoutResource(int row, int column) {
		final int layoutResource;
		switch (getItemViewType(row, column)) {
			case 0:
				layoutResource = R.layout.item_table1_header;
			break;
			case 1:
				layoutResource = R.layout.item_table1_name;
			break;
			case 2:
				layoutResource = R.layout.item_table1;
			break;
			default:
				throw new RuntimeException("wtf?");
		}
		return layoutResource;
	}

	@Override
	public int getItemViewType(int row, int column) {
		if (row < 0) {
			return 0;
		} else if (column < 0) {
			return 1;
		} else {
			return 2;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public View getView(int row, int column, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = getInflater().inflate(getLayoutResource(row, column), parent, false);
		}
		TextView v = (TextView) convertView.findViewById(android.R.id.text1);
		CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
		TextView vP = (TextView) convertView.findViewById(R.id.tvTableItemNamePriority);

		if (getEvent(row) != null) {
			if (getEvent(row).project.length() > 0)
				setTextToView(convertView, R.id.tvProject, '#'+getEvent(row).project);
			if (getEvent(row).context.length() > 0)
				setTextToView(convertView, R.id.tvContext, '#'+getEvent(row).context);
			if (vP != null)
				vP.setBackgroundColor(convertView.getContext().getResources().getColor(Event.getPriorityColor(getEvent(row).priority)));
		}

		setGravity(row,column,v);
		
		setEventRef(row,column,v);
		setEventRef(row,column,cb);

		setListenerForEditEvent(row,column,v);
		setListenerForMenuEvent(row, column, v);
		setListenerForCheckEvent(row,column,cb);

		String val = getCellString(row, column);
		if ((row > -1) && (column > 0)) {
			((LinearLayout) convertView).setBackgroundResource( val.equals("+") ? R.drawable.item_table1_marked : R.drawable.item_table1 );
			v.setText(val.equals("+") ? "" : val );
		}
		else {
			v.setText(val);
		}
		
		if ((row > -1) && (row < events.size())) {
			v.setTag(new EventRef( events.get(row), column, row) );
			if (cb != null)
			{
				cb.setChecked(events.get(row).completed);
				cb.setTag(new EventRef( events.get(row), column, row) );
			}
		}
		
		return convertView;
	}

}
